//素金筛选
var PlaceOrder=false;//代下单
(function(){
	var productTerm={
		init:function(){
			args = GetUrlParms();				
			if(args["team"]!=undefined){						
				 TEAM = args["team"];//2是现货					
			} 
			if(args["cid"]!=undefined){						
				 Customer_id = args["cid"];	
				 PlaceOrder=true;
			} 
			this.commonParamers();
			this.bindEvent();
		},
		bindEvent:function(){
			var self=this;
			if(TEAM==2){
				$(".team_params").show();
				$(".normal_params").hide();
			}else{
				$(".team_params").hide();
				$(".normal_params").show();
			}
			//点击选中                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
			$("#term_page").on("click",".term span",function(){
				$(this).toggleClass("active");
//				//切换类型显示类型细分
//				if($(this).parent().hasClass("type")){
//					var length=$("#Type span.active").length;					
//					if(length==1){
//						var type=$("#Type span.active").attr("data-value");
//						self.changType(type);
//					}else{
//						$("#Type2").remove();
//					}
//				}
			});
			//点击搜索
			$("#filter_product_btn").on("click",function(){
				self.getQueryParameters();				
			});
			//筛选重置
            $(".reset_term_btn").on("click", function () {
                 $(".term span").removeClass("active");//清除其他条件选中的样式
               $(".term-box input").val("");//清除input框里的值
            });
		},
		//供应商输入提示
		loadSupplier:function(){
			  $.ajax({
		         url: "/cg-api/suppliers-dc/?is_classify=1",//提交地址
		         type: "GET",//提交方式
		         success: function (result) {
		         	
		             if (result.result == true) {
						var availableTags = [];
		             	 for (var i = 0; i < result.rows.length; i++) {
		             	 	var row=result.rows[i];
		             	 	if(row.classify.emeralds==true){
		             	 		var supplier_name=row.short_name?(row.short_name):(row.supplier);
		             	 		availableTags.push(supplier_name);
		             	 	}		             	 	
		             	 }
						$("#Supplier").autocomplete({
						 	 source: availableTags
						});
		             }		            		
		         }
		     });
		},
		//公共的筛选参数
		commonParamers:function(){
			//类型
			var Type = '<h4>'+_("品类")+'</h4>'
			for (var i = 0; i < prime_gold[1].Type.length; i++) {
					Type += '<span data-value="'+prime_gold[1].Type[i][0]+'">'+ _(prime_gold[1].Type[i][1]) +'</span>'
			}
			$("#Type").html(Type);
			//材质
			var Material='<h4>'+_("材质")+'</h4>'
			for (var i = 0; i < prime_gold[1].Material.length; i++) {
				Material += '<span data-value="'+prime_gold[1].Material[i][0]+'">'+ _(prime_gold[1].Material[i][1]) +'</span>'
			}
			$("#Material").html(Material);
			//材质
			var AboutWeight='<h4>'+_("约重")+'(g)</h4>'
			for (var i = 0; i < prime_gold[1].AboutWeight.length; i++) {
				AboutWeight += '<span data-value="'+prime_gold[1].AboutWeight[i][0]+'">'+_(prime_gold[1].AboutWeight[i][1]) +'</span>'
			}
			$("#AboutWeight").html(AboutWeight);
			//工艺造型
			var Category='<h4>'+_("工艺造型")+'</h4>'
			for (var i = 0; i < prime_gold[1].Category.length; i++) {
				Category += '<span data-value="'+prime_gold[1].Category[i][0]+'">'+ _(prime_gold[1].Category[i][1]) +'</span>'
			}
			$("#Category").html(Category);
		},
		//获取参数
        getQueryParameters: function () {
           var params = {};
            params.limit = "500";
            params.useDict = true;
            isSearching = true;
            var pageSize = 1;//500
            //查询开始时间
            var dtSearchStart = new Date().getTime();
            //循环找出筛选框里选中的参数
            $("#emerald_term_box .term").each(function (i, item) {
            		 var field = $(item).attr("data-name");
			        var dataType = $(item).attr("data-type");
			        if (field) {
			            var values = [];
			            $(item).find("span.active").each(function (j, p) {
			                var value = $(p).attr("data-value");
			                value = value ? value : $(p).text();
			                value = dataType == 'range' ? value.split('-') : value;
			                values.push(value);
			            });
			
			            if (values.length > 0) {
			                if (dataType == 'range') {
			                    if (!params.between) {
			                        params.between = {};
			                    }
			                    params.between[field] = values;
			                } else {
			                    params[field] = values;
			                }
			            }
			        }              
           });          
		    var supplier = $("#Supplier").val();
		    if (supplier!="") {
		        params.shortName = supplier;
		    }
		    var OriginSN=$("#OriginSN").val();
		    if(OriginSN!=""){
		    	params.OriginSN=OriginSN;
		    }
		    params.Category=1;//暂时只有翡翠
			
			if(TEAM==0){
				var url_href="/Customer/PrimeGold?team=0";//客户端的统包页面
			}else if(TEAM==1){
				var url_href="/Team/PrimeGold?team=1";//团队端的统包页面
			}else if(TEAM==2){
				var url_href="/Team/MyPrimeGold?team=2";//团队端的现货统包页面
			}else if(TEAM==3){
				var url_href="/Team/PrimeGold?team=3&cid="+Customer_id;//团队端的代下单
			}
			var action=url_href;
		  	form = $("<form method='post' id='params_form' action='"+action+"'>"+
		   	"<input type='text' value='"+JSON.stringify(params)+"' name='params'></form>")
			$("#form_box").html(form);
			$("#params_form").submit();
        },
	}
	productTerm.init();
})();
